﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using TimKen.BussLogic;

namespace TimKen.Web.Admin.ProductCategory
{
    public partial class ProductCategory_Test : TimKen.App.AdminPage
    {
        PCategoryManager categoryManager = new PCategoryManager();
        public override string ActionCode
        {
            get
            {
                return "89";
            }
        }

        public int ParentID
        {
            get
            {
                int _ParentID = -1;
                if (Request["id"] != null)
                {
                    _ParentID = Convert.ToInt32(Request["id"]);
                }
                return _ParentID;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["Action"] != null)
                {
                    string action = Convert.ToString(Request["Action"]);
                    string val = Convert.ToString(Request["value"]);
                    int Type = Convert.ToInt32(Request["type"]);
                    string reset = "false";
                    bool Result = false;
                    switch (action)
                    {
                        case "Delete":
                            if (categoryManager.Delete(Convert.ToInt32(val)))
                                reset = "true";
                            break;
                        case "HotKeys":
                            if (Type == 0)
                                Result = categoryManager.SetPHotKeyWords("Add", 1, LanguageID, Convert.ToInt32(val));
                            else
                                Result = categoryManager.SetPHotKeyWords("Delete", 1, LanguageID, Convert.ToInt32(val));
                            if (Result)
                                reset = "true";
                            break;
                        case "SortOrder":
                            if (Type == 0)
                                Result = categoryManager.SetPHotKeyWords("SortOrder", 0, LanguageID, Convert.ToInt32(val));
                            else
                            {
                                Result = categoryManager.SetPHotKeyWords("Delete", 1, LanguageID, Convert.ToInt32(val));
                                if (Result)
                                    Result = categoryManager.SetPHotKeyWords("Add", 1, LanguageID, Convert.ToInt32(val));
                            }
                            if (Result)
                                reset = "true";
                            break;
                        case "SortDown":
                            Result = categoryManager.SetPHotKeyWords("SortDown", Type, LanguageID, Convert.ToInt32(val));
                            if (Result)
                                reset = "true";
                            break;
                        case "SortTop":
                            Result = categoryManager.SetPHotKeyWords("SortTop", Type, LanguageID, Convert.ToInt32(val));
                            if (Result)
                                reset = "true";
                            break;
                        default:
                            break;
                    }
                    ShowWrite(reset);
                }
                else
                {
                    BindTreeListView();
                }
            }
        }

        private void BindTreeListView()
        {
            DataSet ds = categoryManager.GetTreeGrid(LanguageID, ParentID);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
            {
                string str = DataTableToJson(ds.Tables[0]);
                ShowWrite(str);
            }
        }

        private void ShowWrite(string str)
        {
            //str = "[{\"0\":\"1\",\"id\":\"1\",\"1\":\"0\",\"parentId\":\"0\",\"2\":\"Computers\",\"name\":\"Computers\",\"3\":null,\"quantity\":null,\"4\":null,\"price\":null,\"state\":\"closed\",\"total\":0},{\"0\":\"2\",\"id\":\"2\",\"1\":\"0\",\"parentId\":\"0\",\"2\":\"Electronics\",\"name\":\"Electronics\",\"3\":null,\"quantity\":null,\"4\":null,\"price\":null,\"state\":\"closed\",\"total\":0}]";
            //Response.Charset = "GB2312";
            //Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.Clear();
            Response.Buffer = false;
            Response.Charset = "UTF-8";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            Response.Write(str);
            Response.End();
        }

        private string DataTableToJson(DataTable dt)
        {
            StringBuilder jsonBuilder = new StringBuilder();
            jsonBuilder.Append("[");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                jsonBuilder.Append("{");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    jsonBuilder.Append("\"" + dt.Columns[j].ColumnName + "\"");
                    jsonBuilder.Append(":\"");
                    jsonBuilder.Append(dt.Rows[i][j].ToString().Trim());
                    jsonBuilder.Append("\",");
                }
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                jsonBuilder.Append("},");
            }
            if (jsonBuilder.ToString() != "[")
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
            jsonBuilder.Append("]");
            return jsonBuilder.ToString();
        }
    }
}